package com.sdcqjy.property.utils;

import android.content.Context;

import com.google.gson.Gson;
import com.sdcqjy.mylibrary.PrivateFileUtils;
import com.sdcqjy.mylibrary.TimeUtils;
import com.sdcqjy.property.AppLL;
import com.sdcqjy.property.mode.LawRegulationsDetailsMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 磁磁帅 on 2018/7/8.
 * 功能：书签工具
 */
public class GetBookMarkUtils {
    private GetBookMarkUtils() {
    }

    private static GetBookMarkUtils bookMarkUtils;

    public static GetBookMarkUtils getInstance() {
        if (bookMarkUtils == null) bookMarkUtils = new GetBookMarkUtils();
        return bookMarkUtils;
    }

    public void setBookMark(List<LawRegulationsDetailsMode> modeList) {
        BookMarkMode mode = new BookMarkMode();
        mode.time = System.currentTimeMillis();
        mode.modeList = modeList;
        BookMarkMode.setMode(AppLL.getAppLL(), mode);
    }

    public void addBookMark(LawRegulationsDetailsMode detailsMode) {
        BookMarkMode mode = BookMarkMode.getMode(AppLL.getAppLL());
        mode.modeList.add(0, detailsMode);
        BookMarkMode.setMode(AppLL.getAppLL(), mode);
    }

    public void delBookMark(LawRegulationsDetailsMode detailsMode) {
        BookMarkMode mode = BookMarkMode.getMode(AppLL.getAppLL());
        List<LawRegulationsDetailsMode> modeList = mode.modeList;
        for (int i = 0; i < modeList.size(); i++) {
            LawRegulationsDetailsMode lawRegulationsDetailsMode = modeList.get(i);
            if (lawRegulationsDetailsMode.lawID == detailsMode.lawID) {
                modeList.remove(i);
                break;
            }
        }
        BookMarkMode.setMode(AppLL.getAppLL(), mode);
    }

    public List<LawRegulationsDetailsMode> getBookMark() {
        BookMarkMode mode = BookMarkMode.getMode(AppLL.getAppLL());
        long l = System.currentTimeMillis() - mode.time;
        List<LawRegulationsDetailsMode> modeList = null;
        if (l < TimeUtils._24H) {
            modeList = mode.modeList;
        }
        return modeList;
    }

    public static class BookMarkMode {
        private long time;
        private List<LawRegulationsDetailsMode> modeList;
        //-------------保存数据-------------
        private static BookMarkMode mode;
        private static final String jsonName = "LoginMode.json";

        public static BookMarkMode getMode(Context context) {
            if (mode == null) {
                try {
                    String txt = new PrivateFileUtils(context, jsonName).getString();
                    mode = new Gson().fromJson(txt, BookMarkMode.class);
                } catch (Exception e) {
                }
                if (mode == null) {
                    mode = new BookMarkMode();
                    mode.time = 0;
                }
            }
            if (mode.modeList == null) mode.modeList = new ArrayList<>();
            return mode;
        }

        public static void setMode(Context context, BookMarkMode mode) {
            if (mode == null) return;
            BookMarkMode.mode = mode;
            try {
                String s = new Gson().toJson(mode);
                new PrivateFileUtils(context, jsonName).setString(s);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public static void clear(Context context) {
            mode = null;
            new PrivateFileUtils(context, jsonName).delete();
        }
    }
}
